<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			#btn{
				position: fixed;
				top: 600px;
				left: 30;
				color: skyblue;
			}
		</style>
	</head>
	<body style="height: 2000px;">
		<div id="box">
			
		</div>
		<button type="button" id="btn">回到顶部</button>
		<script type="text/javascript">
			window.onscroll = function(){
				// console.log(document.documentElement.scrollTop)
				// 除了苹果的浏览器，需要这样写
				// console.log(document.body.scrollTop)
				// 兼容代码
				var docScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
				console.log(docScrollTop)
			}
			var btn = document.getElementById('btn')
			btn.onclick = function(){
				if(document.documentElement.scrollTop || document.body.scrollTop){
					document.documentElement.scrollTop = 0;
					document.body.scrollTop = 0;
				}
			}
		</script>
	</body>
</html>
